package org.testis.webautomationmodule.services;

import org.testis.data.TestStepException;
import org.testis.data.TestStepResultType;
import org.testis.data.parser.AtofModuleParameterNames;
import org.testis.logging.AtofLogLevel;
import org.testis.logging.LogSenderService;
import org.testis.webautomationmodule.data.WebAutomationModuleDataObject;
import org.testis.webautomationmodule.data.WebAutomationModuleDataNode;
import org.testis.webautomationmodule.ui.WebAutomationTestStepBuilder;
import org.testis.webautomationmodule.ui.WebAutomationDecorator;

/**
 * A class to provide selenium automation service
 * @author nurhat
 */
public class SeleniumWebAutomationModuleProviderService extends WebAutomationModuleProviderService {

    public SeleniumWebAutomationModuleProviderService() {
        super(new SeleniumWebAutomationProvider());

    }

    /**
     *  to start a web automation test, initially the web browser must be opened.
     * This method tries to open the web browser and navigate to default url of 
     * provided dut.
     * @return If browser started successfully and default page is opened, this method return true
     * otherwise returns false
     */
    public boolean openWindow() throws Exception {

        WebAutomationModuleDataObject data = new WebAutomationModuleDataObject();

        String dutIp = "";
        if (getDataObj().getModuleDataToApply().containsKey(
                AtofModuleParameterNames.WEB_AUTO_DEVICE_IP)) {
            dutIp = getDataObj().
                    getModuleDataToApply().get(
                    AtofModuleParameterNames.WEB_AUTO_DEVICE_IP);
        }
        String windowId = "window_" + (int) (2000.0 * Math.random()) + 1;
        data.getLoginDataList().add(new WebAutomationModuleDataNode(
                "WaitForCondition",
                new Object[]{"var " +
                    windowId +
                    " = window.open(\"" +
                    "http://" + dutIp +
                    "\", '" +
                    windowId +
                    "');selenium.browserbot.openedWindows['" +
                    windowId +
                    "'] = " +
                    windowId +
                    ";",
                    "15000"
                }));
        data.getLoginDataList().add(new WebAutomationModuleDataNode(
                "WaitForPopup",
                new Object[]{
                    windowId,
                    "15000"
                }));
        data.getLoginDataList().add(new WebAutomationModuleDataNode("UIWindow",
                new Object[]{
                    windowId
                }));


        WebAutomationTestStepBuilder ts =
                new WebAutomationTestStepBuilder(
                m_automationImpl);

        WebAutomationDecorator decorator = ts.createTestStep(data.getLoginDataList(),
                getDataObj().
                getMessageId());
        if (decorator != null) {
            if (decorator.operate()) {
                LogSenderService.putMessageOnQueue(
                        "SeleniumWebAutomationProviderService",
                        AtofLogLevel.DEBUG,
                        "Test started. Browser is ready for testing...",
                        getDataObj().getMessageId());
                m_automationImpl.currentWindowId = windowId;
                m_automationImpl.windowMaximize();
                return true;
            } else {
                throw new TestStepException(
                        TestStepResultType.FAULT,
                        "Test could not started(SeleniumWebAutomationProviderService)...");
            }
        } else {
            throw new TestStepException(
                    TestStepResultType.FAULT,
                    "Test could not started. Could not create object decorator(SeleniumWebAutomationProviderService)...");
        }
    }

    /**
     * Method sets selenium server's pre-conditions and prepares selenium-rc before running
     * the test step.
     * @throws java.lang.Exception
     */
    public void startAutomation() throws Exception {
        try {
            String dutIp = "";
            String browserType = "*chrome";
            String browserSpeed = "600";
            if (getDataObj().getModuleDataToApply().containsKey(
                    AtofModuleParameterNames.WEB_AUTO_DEVICE_IP)) {
                dutIp = getDataObj().
                        getModuleDataToApply().get(
                        AtofModuleParameterNames.WEB_AUTO_DEVICE_IP);
            } else if (getDataObj().getModuleDataToApply().containsKey(
                    AtofModuleParameterNames.WEB_AUTO_DEVICE_TESTBED_IP)) {
                dutIp = getDataObj().
                        getModuleDataToApply().get(
                        AtofModuleParameterNames.WEB_AUTO_DEVICE_TESTBED_IP);
            }

            if (getDataObj().getModuleDataToApply().containsKey(
                    AtofModuleParameterNames.WEB_AUTO_BROWSER_TYPE)) {
                browserType = getDataObj().
                        getModuleDataToApply().get(
                        AtofModuleParameterNames.WEB_AUTO_BROWSER_TYPE);
            }
            if (getDataObj().getModuleDataToApply().containsKey(
                    AtofModuleParameterNames.WEB_AUTO_BROWSER_SPEED)) {
                browserSpeed = getDataObj().
                        getModuleDataToApply().get(
                        AtofModuleParameterNames.WEB_AUTO_BROWSER_SPEED);
            }
            ((SeleniumWebAutomationProvider) m_automationImpl).setUrl("http://" +
                    dutIp);
            ((SeleniumWebAutomationProvider) m_automationImpl).setMode(
                    browserType);
            Thread.sleep(2000);
            m_automationImpl.start();
            m_automationImpl.setSpeed(browserSpeed);
        } catch (Exception ex) {
            throw new TestStepException(
                    TestStepResultType.FAILURE,
                    "(SeleniumWebAutomationProviderService)Test could not started. Selenium could not started..." + ex.getMessage());
        }

    }

    /**
     * Method stops selenium server and selenium-rc 
     */
    public void stopAutomation() {
        try {
            m_automationImpl.stop();
        } catch (Exception ex) {
            LogSenderService.putMessageOnQueue(
                    "SeleniumWebAutomationProviderService",
                    AtofLogLevel.DEBUG,
                    "An exception occured while stopping selenium..." + ex.getMessage(),
                    getDataObj().getMessageId());
        }
    }
}



